//
//  FunnyTableViewCell.m
//  Zaker
//
//  Created by dllo on 16/3/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FunnyTableViewCell.h"

@interface FunnyTableViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) BaseLabel *titleLabel;
@property (nonatomic, retain) NSMutableArray *dataArr;

@end

@implementation FunnyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[BaseLabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        _titleLabel.textAlignment = 1;
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 1000) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.bounces = NO;
        [self.contentView addSubview:_tableView];
        [_tableView registerClass:[FunnyInTableViewCell class] forCellReuseIdentifier:@"cell"];
        
        self.dataArr = [NSMutableArray array];
        
#pragma mark - 通知中心
        /* 通过单例方法获取通知中心对象 */
        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNoti:) name:@"scrollViiew" object:nil];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20);
}

//- (void)handleNoti:(NSNotification *)noti
//{
//    //CGFloat h = [noti.userInfo[@"scroll"] floatValue];
//    //NSLog(@"%f", h);
//    NSArray<FunnyInTableViewCell *> *array = [self.tableView visibleCells];
//    [array enumerateObjectsUsingBlock:^(FunnyInTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        [obj cellOffset];
//    }];
//}

- (void)setModel:(FunnyModel *)model
{
    if (_model != model) {
        _model = model;
    }
    _titleLabel.text = model.title;
    NSArray *arr = model.items;
    _dataArr = [FunnyInModel modelHandlerWithArray:arr];
    [_tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FunnyInModel *model = _dataArr[indexPath.row];
    FunnyInTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = model;
    cell.selectionStyle = 0;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FunnyInModel *model = _dataArr[indexPath.row];
    self.block(model);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
